//
//  DYLoginTextFieldView.m
//  kafuMerchant
//
//  Created by 帝云科技 on 2018/9/10.
//  Copyright © 2018年 帝云科技. All rights reserved.
//

#import "DYLoginTextFieldView.h"

@interface DYLoginTextFieldView ()<UITextFieldDelegate>

@property (nonatomic,strong)UILabel *line;

@property (nonatomic,strong)UIButton *seeButton;

@end

@implementation DYLoginTextFieldView

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        [self.textField addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
        [self initUI];
    }
    return self;
}
-(void)initUI
{
    [self addSubview:self.textField];
    [self addSubview:self.line];
    [self addSubview:self.titleLabel];
    [self addSubview:self.seeButton];
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(0);
        make.height.equalTo(0);
    }];
    
    [self.textField makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(0);
        make.height.equalTo(BILIHEIGHT(50));
        make.width.equalTo(kScreenWidth-BILIWIDTH(18*2));
    }];
    
    [self.line makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(0);
        make.bottom.equalTo(self.textField);
        make.height.equalTo(BILIWIDTH(0.7));
        make.width.equalTo(kScreenWidth-BILIWIDTH(18*2));
    }];
    
    [self.seeButton makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.size.equalTo(CGSizeMake(0, 0));
        make.right.equalTo(0);
    }];
}
-(void)textFieldChange:(UITextField *)textField{
    if (self.textFieldChangeBlock) {
        self.textFieldChangeBlock(textField.text);
    }
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    !self.topBlock ? : self.topBlock();
    
    [self.titleLabel updateConstraints:^(MASConstraintMaker *make) {
         make.top.equalTo(BILIHEIGHT(5));
         make.height.equalTo(BILIHEIGHT(10));
    }];
    
    [self.textField updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(BILIHEIGHT(20));
        make.height.equalTo(BILIHEIGHT(30));
    }];
    
    [self.seeButton updateConstraints:^(MASConstraintMaker *make) {
         make.size.equalTo(CGSizeMake(BILIHEIGHT(50), BILIHEIGHT(50)));
    }];
    
    textField.font=kFont(14);
    self.line.backgroundColor=[UIColor xhq_base];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField.text.length==0) {
        [self.titleLabel updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(BILIHEIGHT(0));
        }];
        
        [self.textField updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(BILIHEIGHT(0));
            make.height.equalTo(BILIHEIGHT(50));
        }];
        
        [self.seeButton updateConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(CGSizeMake(0,0));
        }];
        
         textField.font=kFont(17);
         self.line.backgroundColor=[UIColor xhq_line];
    }
}
-(void)setIsHide:(BOOL)isHide
{
    _isHide=isHide;
    self.seeButton.hidden=isHide;
}
-(void)seeButtonClick
{
    self.seeButton.selected=!self.seeButton.selected;
    if (self.seeButton.selected) {
        self.textField.secureTextEntry=YES;
    }else{
         self.textField.secureTextEntry=NO;
    }
}
-(UITextField *)textField
{
    if (!_textField) {
        _textField=[[UITextField alloc]init];
        _textField.font=kFont(17);
        _textField.delegate=self;
    }
    return _textField;
}

-(UILabel *)line
{
    if (!_line) {
        _line=[UILabel xhq_lineLabel];
        _line.backgroundColor=LoginLineColor;
    }
    return _line;
}

-(UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel=[Utils labelWithTitleFontSize:12 textColor:[UIColor xhq_content] alignment:0];
    }
    return _titleLabel;
}
-(UIButton *)seeButton
{
    if (!_seeButton) {
        _seeButton=[Utils createBtnWithType:UIButtonTypeCustom backgroundColor:ClearColor action:@selector(seeButtonClick) target:self title:nil image:@"icon_mm_open" font:12 textColor:WhiteColor];
        [_seeButton setImage:IMG(@"icon_mm_close") forState:UIControlStateSelected];
        _seeButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
        _seeButton.hidden=YES;
    }
    return _seeButton;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
